package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IEngineeringConstructOrderDetailView;
import com.cloudsigns.factorymanager2.engineering.IView.IReceiveOrBackEngineeringOrderView;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringConstructOrderBean;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.customview.ReadDilalog;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringConstructOrderDetailPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.ReceiveOrBackEngineeringOrderPresenter;
import com.cloudsigns.factorymanager2.project.adapter.ProductInformationAdapter;
import com.cloudsigns.factorymanager2.project.bean.ProductBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/1/14.
 */

public class ReceiveEngineeringConstructOrderActivity extends Activity implements View.OnClickListener, IEngineeringConstructOrderDetailView, IReceiveOrBackEngineeringOrderView {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_project_name, tv_engineering_name, edt_detail_address, edt_dispath_order_company_colon, edt_dispath_order_person_colon, edt_dispath_order_time_colon, edt_completion_date,edt_receive_people_colon,edt_reception_time_colon;
    private ListView lv_add_product, lv_add_construction_content;
    private TextView btn_receive_order, btn_back_order;
    private RelativeLayout rl_engineering_name, rl_add_product;
    private LinearLayout ll_engineering_info_content;
    private ReadDilalog constructEngineeringInfoDialog;
    private ImageView img_project_select,img_add_product,img_add_construction_content;
    private boolean isClick = true;

    private ProductInformationAdapter adapter;
    private List<ProductBean> productBeanList = new ArrayList<>();
    private final String TAG = "ReceiveEngineeringConstructOrderActivity";
    private EngineeringConstructOrderDetailPresenter engineeringConstructOrderDetailPresenter;
    private ReceiveOrBackEngineeringOrderPresenter receiveOrBackEngineeringOrderPresenter;
    private OrderParentBean orderParentBean;
    private EngineeringConstructOrderBean engineeringConstructOrderBean;
    private CommonEnum.STATUS status;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_receive_engineering_consctruct_order);

        engineeringConstructOrderDetailPresenter = new EngineeringConstructOrderDetailPresenter(this);
        receiveOrBackEngineeringOrderPresenter = new ReceiveOrBackEngineeringOrderPresenter(this);
        init();
        Intent mIntent = getIntent();
        if (null != mIntent) {
            orderParentBean = (OrderParentBean) mIntent.getSerializableExtra(ConstantUtil.KEY_1);
        }
        engineeringConstructOrderDetailPresenter.getEngineeringConstructOrderDetail();
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        tv_project_name = findViewById(R.id.tv_project_name);
        tv_engineering_name = findViewById(R.id.tv_engineering_name);
        edt_detail_address = findViewById(R.id.edt_detail_address);
        edt_dispath_order_company_colon = findViewById(R.id.edt_dispath_order_company_colon);
        edt_dispath_order_person_colon = findViewById(R.id.edt_dispath_order_person_colon);
        edt_dispath_order_time_colon = findViewById(R.id.edt_dispath_order_time_colon);
        edt_completion_date = findViewById(R.id.edt_completion_date);
        edt_receive_people_colon=findViewById(R.id.edt_receive_people_colon);
        edt_reception_time_colon=findViewById(R.id.edt_reception_time_colon);
        btn_receive_order = findViewById(R.id.btn_receive_order);
        btn_back_order = findViewById(R.id.btn_back_order);
        rl_engineering_name = findViewById(R.id.rl_engineering_name);
        rl_add_product = findViewById(R.id.rl_add_product);
        ll_engineering_info_content = findViewById(R.id.ll_engineering_info_content);
        img_project_select = findViewById(R.id.img_project_select);
        img_add_product=findViewById(R.id.img_add_product);
        img_add_construction_content=findViewById(R.id.img_add_construction_content);
        lv_add_product = findViewById(R.id.lv_add_product);
        lv_add_construction_content = findViewById(R.id.lv_add_construction_content);


        tv_title.setText(getResources().getText(R.string.engineering_construct));
        img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        img_add_product.setBackgroundResource(R.mipmap.arrow_down_icon);
        img_add_construction_content.setBackgroundResource(R.mipmap.arrow_down_icon);
        rl_back.setOnClickListener(this);
        btn_receive_order.setOnClickListener(this);
        btn_back_order.setOnClickListener(this);
        img_project_select.setOnClickListener(this);
        img_add_product.setOnClickListener(this);
        img_add_construction_content.setOnClickListener(this);

//        productBeanList.add(new ProductBean(null, 0, null, null, 0, null));
//        productBeanList.add(new ProductBean(null, 0, null, null, 0, null));
//        productBeanList.add(new ProductBean(null, 0, null, null, 0, null));

        adapter = new ProductInformationAdapter(this, TAG);
        adapter.setData(productBeanList);
        lv_add_product.setAdapter(adapter);

    }

    private void initData() {
        if (null != engineeringConstructOrderBean) {
            tv_project_name.setText(engineeringConstructOrderBean.getProjectName());
            tv_engineering_name.setText(engineeringConstructOrderBean.getEngineeringName());
            edt_detail_address.setText(engineeringConstructOrderBean.getEngineeringAddress());
            edt_dispath_order_company_colon.setText(engineeringConstructOrderBean.getCompanyId());
            edt_dispath_order_person_colon.setText(engineeringConstructOrderBean.getCompanyName());
            edt_dispath_order_time_colon.setText(DateUtil.getYyyy_Mm_Dd(engineeringConstructOrderBean.getSendOrderTime()));
            edt_completion_date.setText(DateUtil.getYyyy_Mm_Dd(engineeringConstructOrderBean.getCompleteDate()));

            if (null != engineeringConstructOrderBean.getProductList()) {
                productBeanList.addAll(engineeringConstructOrderBean.getProductList());
                adapter.setData(productBeanList);
                adapter.notifyDataSetChanged();
            }
        }
    }


    private void setButton() {
        if (isClick) {
            btn_receive_order.setClickable(false);
            btn_receive_order.setBackgroundResource(R.color.rgb_00B050);
            btn_back_order.setClickable(false);
            btn_back_order.setBackgroundResource(R.color.rgb_C00000);
        } else {
            btn_receive_order.setClickable(false);
            btn_receive_order.setBackgroundResource(R.color.rgb_ffffffff);
            btn_back_order.setClickable(false);
            btn_back_order.setBackgroundResource(R.color.rgb_ffffffff);
        }
    }

    private void receiveOrder() {
        status = CommonEnum.STATUS.RECEIVED;
        isClick = false;
        setButton();
        receiveOrBackEngineeringOrderPresenter.receiveOrbackEngineeringOrder();

    }

    private void backOrder() {
        status = CommonEnum.STATUS.RETURNED;
        isClick = false;
        setButton();
        receiveOrBackEngineeringOrderPresenter.receiveOrbackEngineeringOrder();
    }

    // 工程名称 收缩
    private void showConstrctEngineeringInfoDialog() {
        if (ll_engineering_info_content.getVisibility() == View.VISIBLE) {
            ll_engineering_info_content.setVisibility(View.GONE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            ll_engineering_info_content.setVisibility(View.VISIBLE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_top);
        }

//        constructEngineeringInfoDialog = new ReadDilalog(this);
//        constructEngineeringInfoDialog.createConstructEngineeringDialog();
//
//        constructEngineeringInfoDialog.show();
    }

    // 产品信息 收缩
    private void showConstrctEngineeringInfoSelect() {
        if (lv_add_product.getVisibility() == View.VISIBLE) {
            lv_add_product.setVisibility(View.GONE);
            img_add_product.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            lv_add_product.setVisibility(View.VISIBLE);
            img_add_product.setBackgroundResource(R.mipmap.arrow_top);
        }
    }

    // 施工内容 收缩
    private void showConstrctEngineeringContentSelect() {
        if (lv_add_construction_content.getVisibility() == View.VISIBLE) {
            lv_add_construction_content.setVisibility(View.GONE);
            img_add_construction_content.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            lv_add_construction_content.setVisibility(View.VISIBLE);
            img_add_construction_content.setBackgroundResource(R.mipmap.arrow_top);
        }
    }

    @Override
    public void onClick(View view) {
        TextView tv;
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.btn_receive_order:
                receiveOrder();
                break;
            case R.id.btn_back_order:
                backOrder();
                break;
            // 工程名称 收缩
            case R.id.img_project_select:
                showConstrctEngineeringInfoDialog();
                break;
                // 产品信息 收放
            case R.id.img_add_product:
                showConstrctEngineeringInfoSelect();
                break;
                // 施工内容 收放
            case R.id.img_add_construction_content:
                showConstrctEngineeringContentSelect();
                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (constructEngineeringInfoDialog != null) {
            constructEngineeringInfoDialog.dismiss();
            constructEngineeringInfoDialog = null;
        }


    }


    @Override
    public String getOrderId() {
        if (null != orderParentBean)
            return orderParentBean.getId();
        return "";
    }

    @Override
    public void getEngineeringConstructOrderDetailSuccessed(EngineeringConstructOrderBean engineeringConstructOrderBean) {
        this.engineeringConstructOrderBean = engineeringConstructOrderBean;
        initData();
    }

    @Override
    public void getEngineeringConstructOrderDetailFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public CommonEnum.EngineeringORderCategory getEngineeringDeptOrderCategoryEnum() {
        if (null != orderParentBean)
            return orderParentBean.getCategory();

        return null;
    }

    @Override
    public String getId() {
        if (null != orderParentBean)
            return orderParentBean.getId();
        return "";
    }

    @Override
    public CommonEnum.STATUS getStatus() {
        return status;
    }

    @Override
    public void receiveOrBackOrderSuccessed() {
        if (null != status) {
            if (status == CommonEnum.STATUS.RETURNED)
                Toast.makeText(this, "接收成功！", Toast.LENGTH_SHORT).show();
            else if (status == CommonEnum.STATUS.RETURNED)
                Toast.makeText(this, "退回成功！", Toast.LENGTH_SHORT).show();
        }
        Intent mIntent = new Intent();
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
        finish();
    }

    @Override
    public void receiveOrBackOrderFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
        isClick = true;
        setButton();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
